package com.tengfei.main.question.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tengfei.common.core.domain.PageQuery;
import com.tengfei.common.core.page.TableDataInfo;
import com.tengfei.common.exception.ServiceException;
import com.tengfei.main.question.domain.Objective;
import com.tengfei.main.question.domain.Subjective;
import com.tengfei.main.question.domain.bo.ObjAndSubBo;
import com.tengfei.main.question.domain.bo.ObjectiveBo;
import com.tengfei.main.question.domain.bo.SubjectiveBo;
import com.tengfei.main.question.domain.vo.ObjectiveVo;
import com.tengfei.main.question.domain.vo.SubjectiveVo;
import com.tengfei.main.question.mapper.ObjectiveMapper;
import com.tengfei.main.question.mapper.SubjectiveMapper;
import com.tengfei.main.question.service.IObjAndSubService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 客观题库Service业务层处理
 *
 * @author tengfei
 * @date 2022-07-28
 */
@Service
public class ObjAndSubServiceImpl implements IObjAndSubService {

    @Resource
    private ObjectiveMapper objectiveMapper;
    @Resource
    private SubjectiveMapper subjectiveMapper;

    /**
     * 根据id和类型查询题目详细信息
     *
     * @param quId
     * @param quType
     * @return
     */
    @Override
    public Object queryByIdAndType(Long quId, Integer quType) {
        if (isObj(quType)) {
            return objectiveMapper.selectVoById(quId);
        } else {
            return subjectiveMapper.selectVoById(quId);
        }
    }

    /**
     * 批量删除
     *
     * @param bos
     * @return
     */
    @Transactional
    @Override
    public Boolean deleteByIds(List<ObjAndSubBo> bos) {
        bos.forEach(bo -> {
            if (isObj(bo.getQuType())) {
                // 删除客观题目
                objectiveMapper.deleteById(bo.getQuId());
            } else {
                // 删除主观题目
                subjectiveMapper.deleteById(bo.getQuId());
            }
        });
        return true;
    }

    /**
     * 查询题目列表
     */
    @Override
    public List queryList(ObjAndSubBo bo) {
        if (isObj(bo.getQuType())) {
            return objectiveMapper.selectVoList(buildQueryWrapper(bo, false));
        } else {
            return subjectiveMapper.selectVoList(buildQueryWrapper(bo, false));
        }
    }

    /**
     * 查询题目列表
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo queryQuPageList(ObjAndSubBo bo, PageQuery pageQuery) {
        // 搜索，按难度等级，题目标签，题目内容
        if (isObj(bo.getQuType())) {
            IPage<ObjectiveVo> objectiveVoIPage = objectiveMapper.selectVoPage(pageQuery.build(), buildQueryWrapper(bo, true));
            return TableDataInfo.build(objectiveVoIPage);
        } else {
            IPage<SubjectiveVo> subjectiveVoIPage = subjectiveMapper.selectVoPage(pageQuery.build(), buildQueryWrapper(bo, true));
            return TableDataInfo.build(subjectiveVoIPage);
        }
    }

    // 查询条件
    private LambdaQueryWrapper buildQueryWrapper(ObjAndSubBo bo, boolean hasPage) {
        if (isObj(bo.getQuType())) {
            return new LambdaQueryWrapper<Objective>()
                    .eq(hasPage, Objective::getObjType, bo.getQuType())
                    .eq(ObjectUtil.isNotNull(bo.getQuGrand()), Objective::getObjGrand, bo.getQuGrand())
                    .like(ObjectUtil.isNotNull(bo.getSearchValue()), Objective::getObjContent, bo.getSearchValue())
                    .or()
                    .like(ObjectUtil.isNotNull(bo.getSearchValue()), Objective::getObjTag, bo.getSearchValue())
                    .orderByDesc(Objective::getUpdateTime);
        } else {
            return new LambdaQueryWrapper<Subjective>()
                    .eq(hasPage, Subjective::getSubType, bo.getQuType())
                    .eq(ObjectUtil.isNotNull(bo.getQuGrand()), Subjective::getSubGrand, bo.getQuGrand())
                    .like(ObjectUtil.isNotNull(bo.getSearchValue()), Subjective::getSubContent, bo.getSearchValue())
                    .or()
                    .like(ObjectUtil.isNotNull(bo.getSearchValue()), Subjective::getSubTag, bo.getSearchValue())
                    .orderByDesc(Subjective::getUpdateTime);
        }

    }

    /**
     * 新增客观题
     *
     * @param bo
     * @return
     */
    @Override
    public Boolean insertObjByBo(ObjectiveBo bo) {
        Objective objective = BeanUtil.copyProperties(bo, Objective.class);
        if (ObjectUtil.isNull(objective.getObjGrand())||objective.getObjGrand()<=0) {
            objective.setObjGrand(Math.round((float) (Math.random() * 5) + 1));
        }
        return objectiveMapper.insert(objective) > 0;
    }

    /**
     * 新增主观题
     *
     * @param bo
     * @return
     */
    @Override
    public Boolean insertSubByBo(SubjectiveBo bo) {
        Subjective subjective = BeanUtil.copyProperties(bo, Subjective.class);
        if (ObjectUtil.isNull(subjective.getSubGrand())||subjective.getSubGrand()<=0) {
            subjective.setSubGrand(Math.round((float) (Math.random() * 5) + 1));
        }
        return subjectiveMapper.insert(subjective) > 0;
    }

    /**
     * 修改客观题目
     *
     * @param bo
     * @return
     */
    @Override
    public Boolean updateObjByBo(ObjectiveBo bo) {
        Objective objective = BeanUtil.copyProperties(bo, Objective.class);
        return objectiveMapper.updateById(objective) > 0;
    }

    /**
     * 修改主观题目
     *
     * @param bo
     * @return
     */
    @Override
    public Boolean updateSubByBo(SubjectiveBo bo) {
        Subjective subjective = BeanUtil.copyProperties(bo, Subjective.class);
        return subjectiveMapper.updateById(subjective) > 0;
    }

    // 非空判断+界限判断+是否为客观题
    public boolean isObj(Integer quType) {
        if (quType.intValue() < 1 || quType.intValue() > 5) {
            throw new ServiceException("题目类型不符合规范");
        } else if (quType.intValue() >= 1 && quType.intValue() <= 3) {
            return true;
        } else {
            return false;
        }
    }

    // 判断是否是1~5之间的数字
    // 难度等级1~5
    private boolean isNum(String searchValue) {
        // 正则匹配数字1~5
        Pattern compile = Pattern.compile("[1-5]");
        Matcher matcher = compile.matcher(searchValue);
        boolean matches = matcher.matches();
        if (matches) {
            return true;
        } else {
            return false;
        }
    }
}
